package com.guanmi.ethereum.controller;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.core.methods.response.EthGasPrice;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.protocol.core.methods.response.EthTransaction;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

import com.guanmi.ethereum.constants.GlobalConstants;
import com.guanmi.ethereum.domain.ERC20TokenSolTemplate;
import com.guanmi.ethereum.domain.WalletResponseEntity;
import com.guanmi.ethereum.service.EthereumService;

@RestController
@RequestMapping(value = "/eth")
public class EthereumController {
	private static final Logger L = LoggerFactory.getLogger(EthereumController.class);
	@Autowired
	private  EthereumService ethereumService;
	/**
	 * @Title: getAccountlist
	 * @Description: 获取账户列表
	 * @return List<String>
	 */
	@ResponseBody
    @RequestMapping(value = "getAccountList", method = RequestMethod.POST)
    public WalletResponseEntity<List<String>> getAccountlist()  {
		WalletResponseEntity<List<String>>  result = new WalletResponseEntity<List<String>>();
		try {
			return ethereumService.getAccountlist();
		} catch (Exception e) {
			L.error("获取账户列表", e.getMessage());
			result.setMsg("系统异常");
			result.setStatus(GlobalConstants.SYSTEM_ERROR);
			return result;
		}
    }

	/**
	 * @Title: generateNewWalletFile
	 * @Description: 创建账户
	 * @param password:密码
	 * @return String
	 */
	@ResponseBody
    @RequestMapping(value = "generateNewWalletFile/{password}", method = RequestMethod.POST)
    public WalletResponseEntity<Credentials> generateNewWalletFile(@PathVariable("password")String password)  {
		WalletResponseEntity<Credentials>  result = new WalletResponseEntity<Credentials>();
		try {
			return ethereumService.generateNewWalletFile(password);
		} catch (Exception e) {
			L.error("创建账户", e.getMessage());
			result.setMsg("系统异常");
			result.setStatus(GlobalConstants.SYSTEM_ERROR);
			return result;
		}
    }

	/**
	 * @Title: getBalance
	 * @Description: 获取用户余额
	 * @param address:账户
	 * @return BigInteger
	 */
	@ResponseBody
    @RequestMapping(value = "getBalance/{address}", method = RequestMethod.POST)
    public WalletResponseEntity<BigInteger> getBalance(@PathVariable("address") String address) {
		WalletResponseEntity<BigInteger>  result = new WalletResponseEntity<BigInteger>();
		try {
			return ethereumService.getBalance(address);
		} catch (Exception e) {
			L.error("获取用户余额", e.getMessage());
			result.setMsg("系统异常");
			result.setStatus(GlobalConstants.SYSTEM_ERROR);
			return result;
		}
    }

	/**
	 * @Title: getTransactionCount
	 * @Description: 获取用户交易记录数
	 * @param address:账户
	 * @return BigInteger
	 */
	@ResponseBody
    @RequestMapping(value = "getTransactionCount/{address}", method = RequestMethod.POST)
    public WalletResponseEntity<BigInteger> getTransactionCount(@PathVariable("address") String address) {
		WalletResponseEntity<BigInteger>  result = new WalletResponseEntity<BigInteger>();
		try {
			return ethereumService.getTransactionCount(address);
		} catch (Exception e) {
			L.error("获取用户交易记录数", e.getMessage());
			result.setMsg("系统异常");
			result.setStatus(GlobalConstants.SYSTEM_ERROR);
			return result;
		}
    }

	/**
	 * @Title: getTransaction
	 * @Description: 获取交易信息
	 * @param transactionHash:交易的hash值
	 * @return EthTransaction
	 */
	@ResponseBody
    @RequestMapping(value = "getTransaction/{transactionHash}", method = RequestMethod.POST)
    public WalletResponseEntity<EthTransaction> getTransaction(@PathVariable("transactionHash")String transactionHash){
		WalletResponseEntity<EthTransaction>  result = new WalletResponseEntity<EthTransaction>();
		try {
			return ethereumService.getTransaction(transactionHash);
		} catch (Exception e) {
			L.error("获取交易信息", e.getMessage());
			result.setMsg("系统异常");
			result.setStatus(GlobalConstants.SYSTEM_ERROR);
			return result;
		}
	}

	/**
	 * @Description: 获取用户的账户
	 * @param walletfile:密钥文件
	 * @param password：密码
	 * @return String
	 * @throws
	 */
	@ResponseBody
    @RequestMapping(value = "getAddress", method = RequestMethod.POST)
    public WalletResponseEntity<String> getAddress(String walletfile, String password) {
		WalletResponseEntity<String>  result = new WalletResponseEntity<String>();
		try {
			return ethereumService.getAddress(walletfile, password);
		} catch (Exception e) {
			L.error("获取用户的账户", e.getMessage());
			result.setMsg("系统异常");
			result.setStatus(GlobalConstants.SYSTEM_ERROR);
			return result;
		}
    }

	/**
	 * @Title: ethSendRawTransaction
	 * @Description: 转账
	 * @param walletfile:密钥文件
	 * @param password：密码
	 * @param toAddress:接受地址
	 * @param coinNum：数量
	 * @return EthSendTransaction
	 */
	@ResponseBody
    @RequestMapping(value = "ethSendRawTransaction", method = RequestMethod.POST)
    public WalletResponseEntity<EthSendTransaction> ethSendRawTransaction(String walletfile, String password, String toAddress, BigDecimal coinNum){
		WalletResponseEntity<EthSendTransaction>  result = new WalletResponseEntity<EthSendTransaction>();
		try {
			return ethereumService.ethSendRawTransaction(walletfile, password, toAddress, coinNum);
		} catch (Exception e) {
			L.error("ethSendRawTransaction 转账 ", e.getMessage());
			result.setMsg("系统异常");
			result.setStatus(GlobalConstants.SYSTEM_ERROR);
			return result;
		}
	}

	/**
	 * @Title: personalSendTransaction
	 * @Description: 转账
	 * @param fromAddress:转出地址
	 * @param password：密码
	 * @param toAddress:接受地址
	 * @param coinNum：数量
	 * @return EthSendTransaction
	 */
	@ResponseBody
    @RequestMapping(value = "personalSendTransaction", method = RequestMethod.POST)
    public WalletResponseEntity<EthSendTransaction> personalSendTransaction(String fromAddress, String password, String toAddress, BigDecimal coinNum){
		WalletResponseEntity<EthSendTransaction>  result = new WalletResponseEntity<EthSendTransaction>();
		try {
			return ethereumService.personalSendTransaction(fromAddress, password, toAddress, coinNum);
		} catch (Exception e) {
			L.error("ethSendRawTransaction 转账 ", e.getMessage());
			result.setMsg("系统异常");
			result.setStatus(GlobalConstants.SYSTEM_ERROR);
			return result;
		}
	}
	/**
	 * 部署合约
	 * @param deployeraccount 合约账号
	 * @param initialSupply 总量
	 * @param tokenName 合约名称
	 * @param tokenSymbol 合约标识
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "deployContract", method = RequestMethod.POST)
    public WalletResponseEntity<ERC20TokenSolTemplate> deployContract(String deployeraccount, BigDecimal initialSupply,
			String tokenName, String tokenSymbol){
		WalletResponseEntity<ERC20TokenSolTemplate>  result = new WalletResponseEntity<ERC20TokenSolTemplate>();
		try {
			return ethereumService.deployContract(deployeraccount, initialSupply, tokenName, tokenSymbol);
		} catch (Exception e) {
			L.error("ethSendRawTransaction 转账 ", e.getMessage());
			result.setMsg("系统异常");
			result.setStatus(GlobalConstants.SYSTEM_ERROR);
			return result;
		}
	}
	
	/**
	 * 部署合约
	 * @param walletfile 合约地址文件名
	 * @param password 密码
	 * @param initialSupply 总量
	 * @param tokenName 合约名称
	 * @param tokenSymbol 合约标识
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "deployContractByPassword", method = RequestMethod.POST)
    public WalletResponseEntity<ERC20TokenSolTemplate> deployContractByPassword(String walletfile, String password ,BigDecimal initialSupply,
			String tokenName, String tokenSymbol){
		WalletResponseEntity<ERC20TokenSolTemplate>  result = new WalletResponseEntity<ERC20TokenSolTemplate>();
		try {
			return ethereumService.deployContract(walletfile, password, initialSupply, tokenName, tokenSymbol);
		} catch (Exception e) {
			L.error("ethSendRawTransaction 转账 ", e.getMessage());
			result.setMsg("系统异常");
			result.setStatus(GlobalConstants.SYSTEM_ERROR);
			return result;
		}
	}
	/**
     * 合约转账
     * @param deployeraccount 合约账号
     * @param contractAddress 合约地址
     * @param toAddress 转账地址
     * @param coinNum 转账数量
     * @return
     */
	@ResponseBody
    @RequestMapping(value = "tokenERC20SendPassword", method = RequestMethod.POST)
    public WalletResponseEntity<TransactionReceipt> tokenERC20SendPassword(String deployeraccount,String password, String contractAddress, String toAddress,
	        BigDecimal coinNum){
		WalletResponseEntity<TransactionReceipt>  result = new WalletResponseEntity<TransactionReceipt>();
		try {
			return ethereumService.tokenERC20SendPassword(deployeraccount,password, contractAddress, toAddress, coinNum);
		} catch (Exception e) {
			L.error("tokenERC20Send 转账 ", e.getMessage());
			result.setMsg("系统异常");
			result.setStatus(GlobalConstants.SYSTEM_ERROR);
			return result;
		}
	}
	
	/**
     * 合约转账
     * @param deployeraccount 合约账号
     * @param contractAddress 合约地址
     * @param toAddress 转账地址
     * @param coinNum 转账数量
     * @return
     */
	@ResponseBody
    @RequestMapping(value = "tokenERC20SendWalletFile", method = RequestMethod.POST)
    public WalletResponseEntity<TransactionReceipt> tokenERC20SendWalletFile(String walletfile,String password, String contractAddress, String toAddress,
	        BigDecimal coinNum){
		WalletResponseEntity<TransactionReceipt>  result = new WalletResponseEntity<TransactionReceipt>();
		try {
			return ethereumService.tokenERC20SendWalletFile(walletfile, password, contractAddress, toAddress, coinNum);
		} catch (Exception e) {
			L.error("tokenERC20SendByPasswrod 转账 ", e.getMessage());
			result.setMsg("系统异常");
			result.setStatus(GlobalConstants.SYSTEM_ERROR);
			return result;
		}
	}

	/**
	 * @Description: 转账
	 * @param walletfile:密钥文件
	 * @param password：密码
	 * @param toAddress:收款账户
	 * @param coinNum：数量
	 * @return TransactionReceipt
	 */
	@ResponseBody
    @RequestMapping(value = "transferSendFunds", method = RequestMethod.POST)
    public WalletResponseEntity<TransactionReceipt> transferSendFunds(String walletfile, String password, String toAddress, BigDecimal coinNum){
		WalletResponseEntity<TransactionReceipt>  result = new WalletResponseEntity<TransactionReceipt>();
		try {
			return ethereumService.transferSendFunds(walletfile, password, toAddress, coinNum);
		} catch (Exception e) {
			L.error("transferSendFunds 转账 ", e.getMessage());
			result.setMsg("系统异常");
			result.setStatus(GlobalConstants.SYSTEM_ERROR);
			return result;
		}
	}



    /**
     * @Title: ethGasPrice
     * @Description:返回当前的gas价格
     * @return EthGasPrice
     */
	@ResponseBody
    @RequestMapping(value = "ethGasPrice", method = RequestMethod.POST)
    public WalletResponseEntity<EthGasPrice> ethGasPrice(){
		WalletResponseEntity<EthGasPrice>  result = new WalletResponseEntity<EthGasPrice>();
		try {
			return ethereumService.ethGasPrice();
		} catch (Exception e) {
			L.error("返回当前的gas价格 ", e.getMessage());
			result.setMsg("系统异常");
			result.setStatus(GlobalConstants.SYSTEM_ERROR);
			return result;
		}
	}


}
